#-------------------------------------------------------#
#   Quality Assessment of the Academic Freedom Index    #
#-------------------------------------------------------#

# Title: "Quality Assessment of the Academic Freedom Index: Strengths, Weaknesses, and How Best to Use It" 
# Authors: "Lott, Lars", "Spannagel, Janika"
# date: 2024-09-19
# journal: Perspectives on Politics
# DOI: TBA
# written under "R and RStudio (4.4.1)"


#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# load packages
library(here)
library(tidyverse)
library(magrittr)
library(reshape2)
library(ggpubr)
library(leaflet)
library(plotly)
library(gapminder)
library(countrycode)
library(ggrepel)
library(ggthemes)
library(lubridate)
library(estimatr)
library(texreg)
library(readxl)
library(haven)
library(scales)
library(fabricatr)

set.seed(1234)

#### Simulate data ####

coder_ids <- readRDS("data/coder_ids.rds")

## coder_characteristics_wide data not included in the replication files due to data protection issues
coder_characteristics_wide <- readRDS("data/coder_characteristics_wide.rds")

coder_characteristics_wide2 <- resample_data(coder_characteristics_wide)

coder_characteristics_wide2 <- coder_characteristics_wide2 %>%
  dplyr::select(-coder_id)
coder_characteristics_wide2$coder_id <- coder_ids

coder_characteristics_wide2 <- coder_characteristics_wide2 %>%
  dplyr::select(coder_id, age_block, gender, government_employment, phd, main_country_id, bin_reside, reside)

summary(coder_characteristics_wide2)
summary(coder_characteristics_wide)
  
main_countries <- sort(coder_characteristics_wide$main_country_id, decreasing = FALSE)
main_countries2 <- sort(coder_characteristics_wide2$main_country_id, decreasing = FALSE)

setdiff(main_countries, main_countries2)
setdiff(main_countries2, main_countries)

saveRDS(coder_characteristics_wide2, file.path("data/coder_characteristics_wide_simulated_data.rds"))

